package com.ssi.myshop.Presenter.impl;

import android.content.Context;
import android.util.Log;

import com.ssi.myshop.Presenter.IAddToCarPresenter;
import com.ssi.myshop.base.BaseData;
import com.ssi.myshop.base.BaseObserver;
import com.ssi.myshop.bean.AddCarBean;
import com.ssi.myshop.contants.Api;
import com.ssi.myshop.utils.RetrofitManager;
import com.ssi.myshop.utils.RxUtil;
import com.ssi.myshop.view.IAddToCarCallBack;

import java.util.HashMap;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class IAddToCarPresenterImpl implements IAddToCarPresenter {
    private static final String TAG = "IAddToCarPresenterImpl";
    private IAddToCarCallBack mCallBack;
    private Retrofit mRetrofit;
    private Api mApi;

    @Override
    public void addToCar(Context context, Integer goodsId) {
        mRetrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = mRetrofit.create(Api.class);
        HashMap<String, Object> params = new HashMap<>();
        String token = context.getSharedPreferences("mysp", Context.MODE_PRIVATE).getString("token", "");
        params.put("token", token);
        params.put("goodsId", goodsId);
        params.put("number", 1);
        Observable<BaseData<AddCarBean>> observable = mApi.addToCar(params);
        observable.compose(RxUtil.schedulerTransformer()).subscribeWith(new BaseObserver<AddCarBean>(mCallBack) {
            @Override
            protected void onSuccess(AddCarBean data) {
                Log.d(TAG, "onSuccess: ");
                mCallBack.addToCarSuccess();
            }
        });
    }

    @Override
    public void registerViewCallBack(IAddToCarCallBack callBack) {
        mCallBack = callBack;
    }

    @Override
    public void unregisterViewCallBack(IAddToCarCallBack callBack) {
        if (mCallBack != null) {
            mCallBack = null;
        }
    }
}
